<template>
  <div class="h-full flex items-center justify-center">
    <a-spin size="large" tip="登录中..."></a-spin>
  </div>
</template>
<script setup lang="ts">
import userApi, { LoginRes } from '@/api/user.ts'
import { useAuthStore } from '@/store'
import { message as Message } from 'ant-design-vue'

const authStore = useAuthStore()
const router = useRouter()
const route: any = useRoute()

const onLoginSuccess = (res: LoginRes) => {
  authStore.setToken(res)
  try {
    if (route.query.redirect) {
      const path = route.query.redirect
      delete route.query.redirect
      router.push({ path: path as string, query: route.query })
    } else {
      router.push('/')
    }
    Message.success('登录成功')
  } catch (error) {
    console.error(error)
  }
}

onMounted(() => {
  userApi.autoLogin({ type: route.query.type, ticket: route.query.ticket }).then(({ data }) => {
    onLoginSuccess(data)
  })
})
</script>

<style scoped></style>
